package chatroom.location;

import chatroom.domain.User;

public class Distance {
	
	public static Double calculateDistance(User user1, User user2){
		Double multiplyLat = new Double(10000/90);
		Double lat1 = user1.getLatitude() * multiplyLat;
		Double lat2 = user2.getLatitude() * multiplyLat;
		
		Double multiplyLong = new Double(40000/360);
		Double long1 = user1.getLongitude() * multiplyLong;
		Double long2 = user2.getLongitude() * multiplyLong;
		Double diffLatitude = lat1 - lat2; 
		Double diffLongitude = long1 - long2; 
		
		Double temp = Math.pow(diffLatitude, 2) + Math.pow(diffLongitude, 2);
		//System.out.println(temp);
		return new Double(Math.sqrt((temp).doubleValue())); 
	}
	
	public static double calculate2(User user1, User user2){
		int R = 6371;
		double dLat = Math.toRadians(user2.getLatitude() - user1.getLatitude());
		double dLon = Math.toRadians(user2.getLongitude() - user1.getLongitude());
		double lat1 = Math.toRadians(user1.getLatitude());
		double lat2 = Math.toRadians(user2.getLatitude());

		double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
		double d = R * c;
		return d; 
	}

}
